<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "stock");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style >
	#outStockInfoCopy{display:none}
	td input{height:21px; width:98%;border:none;}
	td{height:30px;padding: 2px 2px !important;text-align:center }
	#area{
		height: 343px;
		overflow-y:auto; 
		overflow-x:hidden; 
	}
</style>	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
    <div class="detailed-navbar-content panel-body">
    	<input type="hidden" value='${inStockJson}' id="inStockJson" />
        <t:form modelAttribute="inStock" action="/purchase/returnPurchase/saveOutStock" method="post" cssClass="form-horizontal">
            <div class="hide">
                <t:hidden path="inStockId"/>
                <t:hidden path="deleteMark"/>   
                <t:hidden path="type"/>   
                <t:hidden path="createUserName"/>
		 		<t:hidden path="createUserId"/>
		 		<t:hidden path="createDate"/>
		 		<t:hidden path="shopId"/>
            </div>
            <div class="detailed-navbar-content panel-body">
	              <t:formgroup>
	                <t:label path="supplierId" cssClass="col-sm-1" />
                	<t:select2 path="supplierId" placeholder="供应商" allowClear="true" colClass="col-sm-3 col-xs-3"></t:select2>
	                <t:label path="inStockNo" cssClass="col-sm-1" />
	                <t:input path="inStockNo" colClass="col-sm-3" />   
	              	<t:label path="operatorName" cssClass="col-sm-1" />
	                <t:input path="operatorName" colClass="col-sm-3" />
	                
	            </t:formgroup>
	            <t:formgroup>
	               	
	                <t:label path="createDate" cssClass="col-sm-1" />
	                <t:datetime2  inputGroupIcon="icon-calendar" path="createDate" colClass="col-sm-3"></t:datetime2>
	                 <t:label path="comment" cssClass="col-sm-1" />
	               	<t:input path="comment" colClass="col-sm-7" />      
	            </t:formgroup>
	  		</div>
        <div id="area">   
				<table class="cntable table table-bordered dataTable " >
					<thead>
						<tr class="success" >
							<th style="width:80px">序号</th>
							<c:if test="${t_type!=3 }">
								<th style="width:80px">操作</th>
							</c:if>
							<th style="width:150px">商品编码</th>
		            		<th style="width:200px">商品名称</th>
							<th style="width:100px">规格</th>
							<th style="width:100px">单位</th>
							<th style="width:100px">退货数量</th>
							<th style="width:120px">退货单价(元)</th>
							<th>小计(元)</th>	
						</tr>
					</thead>
					<tbody onclick="checkSupplier()" id="outStockInfo">
						<c:set var="sequence"  value="0"/>
						<c:forEach var="inStockShow" items="${inStock.inStockShows}" >
						<c:forEach var="inStockCollocation" items="${inStockShow.inStockCollocations}" >
							<tr id="outStockCollocation_${sequence}" name="outStockCollocation">
								<td style="display:none"><input id="collocationIds_${sequence}" name="collocationIds" type="text" value="${inStockCollocation.collocationId}"/></td>
								<td class="cargoId" style="display:none"><input id="cargoId_${sequence}" name="cargoId" type="text" value="${inStockShow.cargoId}"/></td>
								<td class="cargoType" style="display:none"><input id="cargoType_${sequence}" name="cargoType" type="text" value="${inStockShow.cargo.cargoType}"/></td>
								
								<td name="index" class="elementNum"></td>
								<c:if test="${t_type!=3 }">
									<td id="logo" >
						            	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
						            	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_${sequence}')"><i class="fa fa-minus-square"></i></div>
					            	</td>
								</c:if>
								<td><input style="ime-mode:disabled" readonly="true" id="cargoNo_${sequence}" name="cargoNo" type="text" value="${inStockShow.cargo.cargoNo }"/></td>
				            	<td><input style="ime-mode:disabled" readonly="true" id="cargoName_${sequence}" name="cargoName" type="text" value="${inStockShow.cargo.cargoName}"/></td>
				            	<td><input style="ime-mode:disabled" readonly="true" id="propertyName_${sequence}" name="propertyName" type="text" value="${inStockCollocation.collocation.propertyNameOne }/${inStockCollocation.collocation.propertyNameTwo }"/></td>
								<td >
									<select class="unit" onchange="sumNumber(this,'${purchaseCollocation.collocationId}','${purchaseCollocation.purchasePrice }')"  style="ime-mode:disabled;" id="unit_${sequence}"  name="unit" type="text" value=""><option >${inStockCollocation.unit}</option></select>
								</td>
								<c:if test="${inStockShow.cargo.cargoType!='库存商品'}">
									<td class="price"><input class="form-control" style="ime-mode:disabled;color:red"  onkeyup="return ValidateNumber(this,value)" id="purchaseNums_${sequence}"  name="purchaseNums" type="text" value="${inStockCollocation.collocationNum }"/></td>
									<td class="price"><input onchange="change(this)" class="form-control" style="ime-mode:disabled;color:red"  onkeyup="return ValidateNumber(this,value)" id="purchasePrices_${sequence}" name="purchasePrices" type="text" value="${inStockCollocation.realPrice}"/></td>
									<td class="price"><input class="form-control" style="ime-mode:disabled;color:red" readonly="true'" id="subtotal_${sequence}" name="subtotal" type="text" value="${inStockCollocation.collocationNum*inStockCollocation.realPrice}"/></td>
								</c:if>
								<c:if test="${inStockShow.cargo.cargoType=='库存商品'}">
									<td class="price"><input class="form-control" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchaseNums_${sequence}"  name="purchaseNums" type="text" value="${inStockCollocation.collocationNum }"/></td>
									<td class="price"><input onchange="change(this)" class="form-control" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchasePrices_${sequence}" name="purchasePrices" type="text" value="${inStockCollocation.realPrice}"/></td>
									<td class="price"><input class="form-control" style="ime-mode:disabled" readonly="true" id="subtotal_${sequence}" name="subtotal" type="text" value="${inStockCollocation.collocationNum*inStockCollocation.realPrice}"/></td>
								</c:if>
							</tr>
							<c:set var="sequence"  value="${sequence +1}"/>
							</c:forEach>
							
						</c:forEach>
					</tbody>
					<tfoot >
						<tr  >
							<td>合计</td>
							<td></td>
							<c:if test="${t_type==2 }">
								<td colspan="4"> </td>
							</c:if>
							<c:if test="${t_type==3 }">
								<td colspan="3"> </td>
							</c:if>
							<td><input id="numSubtotal" readonly="true" type="number" style="background:none;outline:none;border:0px"  placeholder="0"></td>
							<td> </td>
							<td><input id="moneySubtotal" readonly="true" type="number" style="background:none;outline:none;border:0px"  placeholder="0.0"></td>
						</tr>
					</tfoot>
				</table>     
				<p id="notice" style="color:red;display:none" >商品不能为空并要填写对应的数量和单价</p>     
			</div>
	  		
        </t:form>
    </div>
    <t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
    <c:if test="${t_type!=3 }">
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</c:if>
	</t:panelFoot>
</t:panel>

<table id="outStockInfoCopy">
	<tbody id="outStockInfoCopyBody">
		<tr id="outStockCollocation_temp" name="outStockCollocation">
			<td style="display:none"><input id="collocationIds_temp" name="collocationIds" type="text" value=""/></td>
			<td style="display:none"><input id="cargoId_temp" name="cargoId" type="text" value=""/></td>
			<td class="cargoType" style="display:none"><input id="cargoType_temp" name="cargoType" type="text" value=""/></td>
			
			<td name="index"></td>
			<td id="logo" >
		          	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
		          	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_temp')"><i class="fa fa-minus-square"></i></div>
		     </td>
								
         	<td colspan="2"><select style="ime-mode:disabled" id="cargoNo_temp" name="cargoNo" type="text" value=""><option value=""></option></select></td>
         	<td style="display:none;"><input style="ime-mode:disabled;" readonly="true" id="cargoNoShow_temp" name="cargoNoShow" type="text" value=""/></td>
         	<td style="display:none;"><input style="ime-mode:disabled" readonly="true" id="cargoName_temp" name="cargoName" type="text" value=""/></td>
         	<td><input style="ime-mode:disabled" readonly="true" id="propertyName_temp" name="propertyName" type="text" value=""/></td>
         	<td >
         		<select class="select2"  style="ime-mode:disabled;"id="unit_temp" name="unit" type="text" value=""></select>
         	</td>
	
			<td class="price"><input style="ime-mode:disabled" class="form-control"  onkeyup="return ValidateNumber(this,value)" id="purchaseNums_temp"  name="purchaseNums" type="text" value=""/></td>
			<td class="price"><input onchange="change(this)" style="ime-mode:disabled" class="form-control"  onkeyup="return ValidateNumber(this,value)" id="purchasePrices_temp" name="purchasePrices" type="text" value=""/></td>
			<td class="price"><input style="ime-mode:disabled" class="form-control" id="subtotal_temp" name="subtotal" type="text" value=""/></td>
		</tr>
	</tbody>
</table>

<script type="text/javascript">

$(function(){
	outStockInfo = $("#outStockInfoCopyBody").html();
	
	
	//注册下拉框选择展开监听事件
	$(".unit").on("select2:open", function (e) { 
		
		var cargoId=$(this).parent().siblings(".cargoId").find("input").val()
		unitInfo=[];
		var select=this;
		productInfoData.forEach(function(element,index){
			
			if(element.cargoId==cargoId){
				if(!$.isNull(element.unit)){
					unitInfo.push({
						id:element.unit,
						text:element.unit,
						ratio:"1",
						mark:element.id
					})
				}
				if(!$.isNull(element.viceFirstUnit)){
					unitInfo.push({
						id:element.viceFirstUnit,
						text:element.viceFirstUnit+"（"+element.firstRatio+"）",
						ratio:element.firstRatio,
						mark:element.id
					})
				}
				if(!$.isNull(element.viceSecondUnit)){
					unitInfo.push({
						id:element.viceSecondUnit,
						text:element.viceSecondUnit+"（"+element.secondRatio+"）",
						ratio:element.secondRatio,
						mark:element.id
					})
				} 
			}

		}); 
		if($(select).find("option").length<2){
			$(select).find("option").remove();
			unitInfo.forEach(function(element,index){
				var newOption = new Option(element.text, element.id, false, false);
				
				$(select).append(newOption);	
			})
			unitInfoForChange=unitInfo;
			unitListForEdit.push(unitInfoForChange)
		}
		
		
	 
	});
	
	
	loadPurchase();
	$('#otherMoney').bind('input propertychange', function() {
		var otherMoney = parseFloat($(this).val());

		if(isNaN(otherMoney)){
			otherMoney=0
			$(this).val("");
		}
		updateBillingMoney();
	});

	indexChange();
	
	iframeLazyOpen();
	tabpaneResize();
	
	updateBillingMoney();
	updateNumSubtotal();
	loadPurchaseNum();
})

function sumNumber(element,collocationId,price){
	
		var unit=$(element).val();
		var elementNum=$(element).parent().siblings(".elementNum").text()-1;
		
		if(unitListForEdit.length>0){
			for(var i=0;i<unitListForEdit.length;i++){
				
					for(var j=0;j<unitListForEdit[i].length;j++){
						if(unitListForEdit[i][j].mark==collocationId){
						if(unitListForEdit[i][j].id==unit){
							
							$('#purchasePrices_'+elementNum).val(price*unitListForEdit[i][j].ratio);
							
							var count=$('#purchaseNums_'+elementNum).val();
							$('#subtotal_'+elementNum).val(price*unitListForEdit[i][j].ratio*count);
						}
					}
				}
			}
		}
		updateBillingMoney();
		updateNumSubtotal();

}

var productInfoData = [];
var unitInfo=[];
var unitInfoForChange=[];
var unitListForEdit=[];
var changeNum=0;



function loadPurchase(){
	var supplierId=$("#supplierId").find("option:selected").val();
	productInfoData=[];
	unitInfo=[];
	cargoInfo=[];
	var puttyNum=0;
	var url =$.getVirtualPath()+"/purchase/purchase/supplierSelect";
	 $.ajax({
	        type: 'get',
	        dataType: "json",
	        url: url,
	        data: "supplierId="+supplierId,
	        async: false,
	        success: function (result) {
	        	data=[];			
				if(!$.isNull(result)){
					data = result;
					cargoInfo=result;
					productInfoData.push({
    					id:"1",
    					text:"",
    					cargoId:"",
    					cargoName:"",
    					cargoNo:"",
    					propertyName:"",
    					supplyPrice:"",
    					cargoType:"",
    					viceFirstUnit:"",
						viceSecondUnit:"",
						firstRatio:"",
						secondRatio:"",
    				})
					result.forEach(function(element,index){		    			
						if(!$.isNull(element.collocation.propertyNameTwo)){
	    					 productInfoData.push({
	    						 id:element.collocation.collocationId,
									text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"*"+element.collocation.propertyNameTwo+"】",
									cargoId:element.cargoId,
									cargoName:element.cargo.cargoName,
									cargoNo:element.cargo.cargoNo,
									cargoType:element.cargo.cargoType,
									unit:element.cargo.unit,
									viceFirstUnit:element.cargo.viceFirstUnit,
									viceSecondUnit:element.cargo.viceSecondUnit,
									firstRatio:element.cargo.firstRatio,
									secondRatio:element.cargo.secondRatio,
									propertyName:element.collocation.propertyNameOne + "/" + element.collocation.propertyNameTwo,
									supplyPrice:element.supplyPrice,
			    				})
	    				 }else{
	    					 productInfoData.push({
	    						 id:element.collocation.collocationId,
									text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"】",
									cargoId:element.cargoId,
									cargoName:element.cargo.cargoName,
									cargoNo:element.cargo.cargoNo,
									cargoType:element.cargo.cargoType,
									unit:element.cargo.unit,
									viceFirstUnit:element.cargo.viceFirstUnit,
									viceSecondUnit:element.cargo.viceSecondUnit,
									firstRatio:element.cargo.firstRatio,
									secondRatio:element.cargo.secondRatio,
									propertyName:element.collocation.propertyNameOne ,
									supplyPrice:element.supplyPrice,
			    				})
	    				 }			
						$("#unit_"+puttyNum).select2();
						puttyNum++;
						
						}); 
					}
	        }
	    });
	
}

function deleteAll(){
	$("tr[name='outStockCollocation']").each(function(){
		if($(this).attr("id")!="outStockCollocation_temp"){
			$(this).remove();
			updateBillingMoney();
			updateNumSubtotal();
			indexChange();
		}
	});
	num=0;
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);
	
	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
	indexChange();
	
}

function checkSupplier(){
	var supplierId=$("#supplierId").find("option:selected").val();	
	if($.isNull(supplierId)){		
		$.showTips("请选择供应商！",1000,"warning");
		return;
	}
}



function iframeLazyOpen(){
	$(".nav.nav-tabs.nav-tabs-component li a").click(function(){
		var iframeTab = $(this).attr("href");
		var iframeId = iframeTab.substring(0,iframeTab.indexOf("-")) + "Iframe";
		$(iframeId).attr("src",$(iframeId).attr("data-src"));
	});
}

function tabpaneResize(){
	$(".tab-pane").height($(window).height() - 50);
	$(window).bind("resize",function(){$(".tab-pane").height($(window).height() - 50)});
}

var outStockInfo;
var newOutStockInfo;
var num=1;
function addData(){
	num++;
	
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);

	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
	indexChange();
}

function deleteData(deleteId){
	if(index!=3){
		$("#"+deleteId).remove();
		updateBillingMoney();
		updateNumSubtotal();
		indexChange();
	}
}

var index;
function indexChange(){
	index=1;
	$("td[name='index']").each(function(){
		$(this).html(index);	
		index++;
	});
}
function saveData(lBtn){
	var isRepetition = false;
	var collocationIds=[];
	$("input[name='collocationIds']").each(function(){
		collocationIds.push({
			id:$(this).val(),
			text:$(this).val()
		})
	});

	for(var i=0;i<collocationIds.length;i++){
		for(var j=i+1;j<collocationIds.length;j++){
			
			if(collocationIds[i].id!=""||collocationIds[j].id!=""){
				if(collocationIds[i].id==collocationIds[j].id){
					isRepetition=true;
				}
			}

		}
	}
	
	var isSave=false;
	$("input[name='collocationIds']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			isSave=true;
		}
						
	});
	if(!isSave){
		$.showTips("请选择开单货品！",1000,"warning");
		return;
	}

	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}	

var showMark=true;

var stuffData=[];
function stocksShow(showNum){
	unitInfo=[];
	var select=$('#cargoNo_'+showNum).select2({placeholder:"请选择货物信息",allowClear: true ,data : productInfoData});
	var select2=$('#unit_'+showNum).select2({placeholder:"请选择单位",allowClear: true ,data : unitInfo});
	select.change(function(){
		unitInfoForChange=[];
		var collocationId = $('#cargoNo_'+showNum).val();
		
		var repetitionNum = 0;
		$("select[name='cargoNo']").each(function(){
			if($(this).val()!=null&&$(this).val()!=""){
				if($(this).val()==collocationId){
					repetitionNum++;
				}
			}

		});
		productInfoData.forEach(function(element,index){
			if(element.id==collocationId){
				if(!$.isNull(element.unit)){
					unitInfo.push({
						id:element.unit,
						text:element.unit,
						ratio:"1",
						mark:element.id
					})
				}
				
				if(!$.isNull(element.viceFirstUnit)){
					unitInfo.push({
						id:element.viceFirstUnit,
						text:element.viceFirstUnit+"（"+element.firstRatio+"）",
						ratio:element.firstRatio,
						mark:element.id
					})
				}
				if(!$.isNull(element.viceSecondUnit)){
					unitInfo.push({
						id:element.viceSecondUnit,
						text:element.viceSecondUnit+"（"+element.secondRatio+"）",
						ratio:element.secondRatio,
						mark:element.id
					})
				}
				unitInfoForChange=unitInfo;
				unitListForEdit.push(unitInfoForChange)
				$('#cargoId_'+showNum).val(element.cargoId);
				$('#collocationIds_'+showNum).val(element.id);
				$('#cargoNoShow_'+showNum).val(element.cargoNo);
				$('#cargoName_'+showNum).val(element.cargoName);
				$('#unit_'+showNum).val(element.unit);
				$('#cargoType_'+showNum).val(element.cargoType);
				$('#propertyName_'+showNum).val(element.propertyName);
				$('#purchaseNums_'+showNum).val(1);
				if(element.supplyPrice!=null && element.supplyPrice != ""){
					$('#purchasePrices_'+showNum).val(element.supplyPrice);
					$('#subtotal_'+showNum).val(element.supplyPrice*1);
				}else{
					$('#purchasePrices_'+showNum).val(0);
					$('#subtotal_'+showNum).val(0);
				}
				
				var select2=$('#unit_'+showNum).select2({placeholder:"请选择单位",allowClear: true ,data : unitInfo});
				select2.change(function (){
					var unit=$(this).val();

					if(unitListForEdit.length>0){
						for(var i=0;i<unitListForEdit.length;i++){
							
								for(var j=0;j<unitListForEdit[i].length;j++){
									if(unitListForEdit[i][j].mark==collocationId){
									if(unitListForEdit[i][j].id==unit){
										
										$('#purchasePrices_'+showNum).val(element.supplyPrice*unitListForEdit[i][j].ratio);
										
										var count=$('#purchaseNums_'+showNum).val();
										$('#subtotal_'+showNum).val(element.supplyPrice*unitListForEdit[i][j].ratio*count);
									}
								}
							}
						}
					}
					updateBillingMoney();
					updateNumSubtotal();
					
				})
				
			}
		});
		
		var cargoType=$('#cargoType_'+showNum).val()
		
		if(cargoType!="库存商品"){
			
			$('#cargoId_'+showNum).parent().siblings(".price").find("input").css("color","red")
			$('#unit_'+showNum).css("color","red");
		}
		
		$("#cargoNoShow_"+showNum).parent().css("display","");
		$("#cargoName_"+showNum).parent().css("display","");
		$("#cargoNo_"+showNum).parent().css("display","none");
		updateBillingMoney();
		updateNumSubtotal();
		if(repetitionNum>1){
			$.showTips("此货品已存在！",1000,"warning");
			deleteData('outStockCollocation_'+showNum);
		}
		addNewRow();
	});
	updateSubtotal(showNum);
	

}
function addNewRow(){
	var isAllow=0;
	var collocationIds=[];
	$("input[name='collocationIds']").each(function(){
		collocationIds.push({
			id:$(this).val(),
			text:$(this).val()
		})
	});

	for(var i=0;i<collocationIds.length;i++){
		if(collocationIds[i].id==""){
			isAllow++;
		}
	}
	if(isAllow<2){
		addData();
	}
	
}

function loadPurchaseNum(){
	if(!$.isNull($("#inStockJson").val())){
		var inStock =JSON.parse($("#inStockJson").val());
		num=0;
		inStock.inStockShows.forEach(function(element,index){
			updateSubtotal(num);
			num++;
		});
	}
		
}
function updateSubtotal(showNum){
	$('#purchaseNums_'+showNum).bind('input propertychange', function() {
		var count = parseFloat($(this).val());
		if(isNaN(count)||$.isNull(count)){
			count = 0;
			$(this).val(0);
		}else{
			var numValue = $(this).val();
			if(numValue.length>1){
				if(numValue.substring(1,2)!="."){
					if(numValue.substring(0,1)=='0'){
						$(this).val(parseFloat(numValue.substring(1,numValue.length)));
					}
				}

			}
		}
		var price = $('#purchasePrices_'+showNum).val();
		$('#subtotal_'+showNum).val(price*count);
		updateBillingMoney();
		updateNumSubtotal();
		
	});
	$('#purchasePrices_'+showNum).bind('input propertychange', function() {
		var price = parseFloat($(this).val());

		if(isNaN(price)||$.isNull(price)){
			price = 0;
			$(this).val(0);
		}else{
			var numValue = $(this).val();
			if(numValue.length>1){
				if(numValue.substring(1,2)!="."){
					if(numValue.substring(0,1)=='0'){
						$(this).val(parseFloat(numValue.substring(1,numValue.length)));
					}
				}

			}
		}
		var count = $('#purchaseNums_'+showNum).val();
		if($(this).parent().siblings(".cargoType").find("input").val()!="库存商品"){
			$('#subtotal_'+showNum).val(-price*count);
			updateBillingMoney();
		}else{
			$('#subtotal_'+showNum).val(price*count);
			updateBillingMoney();
		}
	});
	$('#purchaseNums_'+showNum).focus();
	
}

function updateBillingMoney(){
	var purchaseMoney = 0;
	var moneySubtotal=0;
	var otherMoney = $("#otherMoney").val();
	if(otherMoney != "" && otherMoney != null && otherMoney != undefined && otherMoney!=0){
		purchaseMoney=purchaseMoney+parseFloat(otherMoney);
	}

	$("input[name='subtotal']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			purchaseMoney+= parseFloat($(this).val());
			moneySubtotal+= parseFloat($(this).val());
		}
						
	});
	$("#purchaseMoney").val(purchaseMoney);
	$("#moneySubtotal").val(moneySubtotal);
    
}

function updateNumSubtotal(){
	var numSubtotal=0;
	$("input[name='purchaseNums']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			numSubtotal+= parseFloat($(this).val());
		}
						
	});
	$("#numSubtotal").val(numSubtotal);
	$("#purchaseNum").val(numSubtotal);
}

function ValidateNumber(e, pnumber){
	if (!/^\d+[.]?\d*$/.test(pnumber))
	{
	e.value = /^\d+[.]?\d*/.exec(e.value);
	}else{
		
 	}
	return false;
}

function change(element){
	var e=element.value
	if($(element).parent().siblings(".cargoType").find("input").val()!="库存商品"){
		$(element).val(-e)
	}
	
}

function addPurchaseCollocation(){
	var supplierId=$("#supplierId").find("option:selected").val();	
	if($.isNull(supplierId)){		
		$.showTips("请选择供应商！",1000,"warning");
		return;
	}
	top.openModal("/shop/supplyCargo/collocation/index?supplierId="+$("#supplierId").val()+"&type="+"addPurchaseCollocation","supplyCargo_form","商品-添加",1000,500);
}
function assetModalClose(){
	var supplierId=$("#supplierId").find("option:selected").val();
	var newProductInfoData=[];
	cargoInfo=[];
	var url =$.getVirtualPath()+"/purchase/purchase/supplierSelect";
	$.ajax({
		type: 'get',
		dataType: "json",
		url: url,
		data: "supplierId="+supplierId,
		async: false,
		success: function (result) {
	     	data=[];			
			if(!$.isNull(result)){
				data = result;
				cargoInfo=result;
				result.forEach(function(element,index){		    			
					puttyNum = 0;

					newProductInfoData.push({
	  					id:element.collocation.collocationId,
	  					text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"*"+element.collocation.propertyNameTwo+"】",
	  					cargoId:element.cargoId,
	  					cargoName:element.cargo.cargoName,
	  					unit:element.cargo.unit,
	  					cargoNo:element.cargo.cargoNo,
	  					propertyName:element.collocation.propertyNameOne + "/" + element.collocation.propertyNameTwo,
	  					supplyPrice:element.supplyPrice,
	    			})
				}); 
			}
		}
	});
	
	for(var j=0;j<productInfoData.length;j++){
		for(var i=0;i<newProductInfoData.length;i++){
			if(newProductInfoData[i].id==productInfoData[j].id){
				newProductInfoData.splice(i,1);
				
				i=i-1;
			} 
		}
	}
	newProductInfoData.forEach(function(element,index){
		productInfoData.push({
			id:element.id,
			text:element.text,
			cargoId:element.cargoId,
			cargoName:element.cargoName,
			cargoNo:element.cargoNo,
			unit:element.unit,
			propertyName:element.propertyName,
			supplyPrice:element.supplyPrice,
		})
	});
	$("select[name='cargoNo']").each(function(){
		if($(this).attr("id")!="cargoNo_temp"){
			var select=$(this).select2({placeholder:"请选择货物信息",allowClear: true ,data : newProductInfoData});
		}
	});

}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
